#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>

#include <sys/shm.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

#define SHM_NAME "./sha_file"
#define SHM_SIZE 4096

int main()
{
    __pid_t pid;
    int shmid;
    key_t key;
    void *addr;
    char buffer[1024];
    key = ftok(SHM_NAME, 's');

    shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT);

    if (shmid < 0)
    {
        perror("shmget error!");
        exit(1);
    }

        printf("key = %x\n", key);
        printf("shmid = %d\n", shmid);

        addr = shmat(shmid, NULL, 0);
    if (addr == (void *)-1)
    {
        perror("shm at error!");
        exit(1);
    }

    while (1)
    {
        memset(buffer, 0, sizeof(buffer));
        memset(buffer, 0, sizeof(buffer));
        memcpy(buffer, addr, strlen((char *)addr));
        printf("recv data = %s\n",buffer);
        memset(addr,0,SHM_SIZE);

        // pause();

        // memcpy(buffer, addr, strlen((char *)addr));
        // printf("recv data = %s\n", buffer);
        // memset(addr, 0, SHM_SIZE);

        sleep(1);
    }

    return 0;
}